<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="商户" prop="deptId">
        <el-select v-model="queryParams.deptId" filterable placeholder="请选择商户">
              <el-option
                  v-for="item in deptOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
              ></el-option>
            </el-select>
      </el-form-item>
      <el-form-item label="加入购物车时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购物车状态" prop="cartStatus">
        <el-select v-model="queryParams.cartStatus" placeholder="请选择购物车状态" clearable>
          <el-option
            v-for="dict in bl_cart_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="false">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['bl:cart:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bl:cart:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bl:cart:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['bl:cart:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cartList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="false"/>
      <el-table-column label="购物车表" align="center" prop="cartId" v-if="false"/>
      <el-table-column label="供应商" align="center" prop="dept.deptName" />
      <el-table-column label="加入购物车的时间" align="center" prop="addTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.addTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购物车状态" align="center" prop="cartStatus">
        <template #default="scope">
          <dict-tag :options="bl_cart_status" :value="scope.row.cartStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="用户id" align="center" prop="userId" v-if="false"/>
      <el-table-column label="session" align="center" prop="sessionId" v-if="false"/>
      <el-table-column label="商品id" align="center" prop="goodsId" v-if="false"/>
      <el-table-column label="库存ID" align="center" prop="storeId" v-if="false"/>
      <el-table-column label="商品货号" align="center" prop="goodsSn" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="购买数量" align="center" prop="goodsNum" />
      <el-table-column label="商品价格" align="center" prop="goodsPrice" />
      <el-table-column label="会员折扣价" align="center" prop="memberGoodsPrice" v-if="false"/>
      <el-table-column label="购物车选中状态" align="center" prop="selected" v-if="false"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="false">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['bl:cart:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['bl:cart:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改购物车对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="cartRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="session" prop="sessionId">
          <el-input v-model="form.sessionId" placeholder="请输入session" />
        </el-form-item>
        <el-form-item label="商品id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="库存ID" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入库存ID" />
        </el-form-item>
        <el-form-item label="商品货号" prop="goodsSn">
          <el-input v-model="form.goodsSn" placeholder="请输入商品货号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="本店价" prop="goodsPrice">
          <el-input v-model="form.goodsPrice" placeholder="请输入本店价" />
        </el-form-item>
        <el-form-item label="会员折扣价" prop="memberGoodsPrice">
          <el-input v-model="form.memberGoodsPrice" placeholder="请输入会员折扣价" />
        </el-form-item>
        <el-form-item label="购买数量" prop="goodsNum">
          <el-input v-model="form.goodsNum" placeholder="请输入购买数量" />
        </el-form-item>
        <el-form-item label="${comment}" prop="originalImg">
          <el-input v-model="form.originalImg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="购物车选中状态" prop="selected">
          <el-input v-model="form.selected" placeholder="请输入购物车选中状态" />
        </el-form-item>
        <el-form-item label="加入购物车的时间" prop="addTime">
          <el-date-picker clearable
            v-model="form.addTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择加入购物车的时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Cart">
import { listCart, getCart, delCart, addCart, updateCart } from "@/api/bl/cart";
import { listDeptAll } from "@/api/bl/dept";


const { proxy } = getCurrentInstance();
const { bl_cart_status } = proxy.useDict('bl_cart_status');

const cartList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const deptOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deptId: null,
    goodsName: null,
    originalImg: null,
    cartStatus: null,
    addTime: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询购物车列表 */
function getList() {
  if(queryParams.value.deptId === null || queryParams.value.deptId === undefined){
    return;
  }
  loading.value = true;
  listCart(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    cartList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    cartId: null,
    deptId: null,
    userId: null,
    sessionId: null,
    goodsId: null,
    storeId: null,
    goodsSn: null,
    goodsName: null,
    goodsPrice: null,
    memberGoodsPrice: null,
    goodsNum: null,
    originalImg: null,
    selected: null,
    cartStatus: null,
    addTime: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("cartRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.cartId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加购物车";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _cartId = row.cartId || ids.value
  getCart(_cartId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改购物车";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["cartRef"].validate(valid => {
    if (valid) {
      if (form.value.cartId != null) {
        updateCart(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCart(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _cartIds = row.cartId || ids.value;
  proxy.$modal.confirm('是否确认删除购物车编号为"' + _cartIds + '"的数据项？').then(function() {
    return delCart(_cartIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('bl/cart/export', {
    ...queryParams.value
  }, `cart_${new Date().getTime()}.xlsx`)
}

    /** 查询商户信息列表 */
function getDeptAllList() {
  listDeptAll(queryParams.value).then(response => {
    let deptListTmp = response.data;
    deptOptions.value = deptListTmp.filter(item => item.deptId !== 100);
  });
}

getDeptAllList();
getList();
</script>
